package com.brkj.search;

import myzxing.CaptureActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.brkj.model.DS_tag;
import com.brkj.main.R;
import com.brkj.httpInterface.HttpInterface.HIF_GetTag;
import com.brkj.util.ActivityStackControlUtil;
import com.brkj.util.JsonUtil;
import com.brkj.util.KeywordsFlow;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.view.BaseActivity;
import com.brkj.util.view.WidgetAnim;
import com.dgl.sdk.util.ShakeListener;
import com.dgl.sdk.util.ShakeListener.OnShakeListener;

public class SearchActivity extends BaseActivity{
	
	@ViewInject(id=R.id.keyFlow) KeywordsFlow keyFlow;
	@ViewInject(id=R.id.inputText) EditText inputText;
	@ViewInject(id=R.id.search,click="click_GetCourseByTag") Button search;
//	@ViewInject(id=R.id.scan,click="click_Scan") Button scan;
	@ViewInject(id=R.id.change) View change;
	private long exitTime;
	private String[] tags;
	
	private Vibrator vibrator;
	public static ShakeListener shakeListener;
	private static final String TAG = "SearchActivity";
	private static final int SENSOR_SHAKE = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.search_course);
		setActivityTitle("知识库");
        setRightBtnIcon(R.drawable.saoyisao_selector);
        rightBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchActivity.this, CaptureActivity.class);
                SearchActivity.this.startActivity(intent);
            }
        });
		
		getTag();
		
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		
		shakeListener = new ShakeListener(this, new OnShakeListener() {
			
			@Override
			public void onShake() {
				shakeListener.stop();
				
				new Handler().postDelayed(new Runnable(){
					@Override
					public void run(){
						changeTagWords();
						shakeListener.start();
					}
				}, 500);
			}
		});

		shakeListener.start();
	}

	
	protected void randomTagSearch() {
		if((tags!= null)&&(tags.length>0)){
			int randomNumber=(int)(Math.random()*tags.length)+1;
			searchCourse(tags[randomNumber]);
		}
		
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		shakeListener.start();
	}


	@Override
	protected void onPause() {
		super.onPause();
		shakeListener.stop();
	}
	
	private void getTag() {
		FinalHttp fh = new FinalHttp();
		
		fh.post(HIF_GetTag.address, new MyAjaxCallBack<Object>(this) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				DS_tag tag = JsonUtil.fromJson((String)t, DS_tag.class);
			
				if(tag != null){
					tags = tag.getTag();
					InitKeyWordsFlow();
				}
			}
			
		});
	}

	public void click_GetCourseByTag(View v){
		if(WidgetAnim.checkIfEmpty(this, inputText)){
			return;
		}
		
		searchCourse(inputText.getText().toString());
	}

    public void click_Scan(View v){

	}
	
	private void searchCourse(String key) {
		Intent intent = new Intent(this, SearchResultActivity.class);
		intent.putExtra("key", key);
		startActivity(intent);
		
	}

	private void InitKeyWordsFlow() {
		keyFlow.setDuration(800l);  
		keyFlow.setOnItemClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (v instanceof TextView) {
					String keyword = ((TextView) v).getText().toString();
					searchCourse(keyword);
				}

			}
		});  
	
		if(tags.length>0){
			KeywordsFlow.feedKeywordsFlow(keyFlow, tags);  
	        keyFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
		}
        
        change.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				changeTagWords();
			}
		});
	}
	
	private void changeTagWords(){
		keyFlow.rubKeywords();
		if((tags != null) && (tags.length>0)){
			KeywordsFlow.feedKeywordsFlow(keyFlow, tags);  
	        keyFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ) {
	        if((System.currentTimeMillis() - exitTime) > 2000){
	            showToast("再按一次退出掌上学苑 ");
	            exitTime = System.currentTimeMillis();
	        } else {
	        	ActivityStackControlUtil.finishProgram();
	        }
		}
		
		return true;
	}
}
